package com.gensdai.leliang.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gensdai.leliang.R;
import com.gensdai.leliang.adapter.OrderInfoProductListAdapter;
import com.gensdai.leliang.adapter.SimpleOrderInfoProductListAdapter;
import com.gensdai.leliang.base.BaseActivity;
import com.gensdai.leliang.common.bean.ApplyResultDetailBean;
import com.gensdai.leliang.entity.parseBean.BaseParse;
import com.gensdai.leliang.remoteInterface.RetrofitFactory;
import com.gensdai.leliang.retrofitUtils.BaseObserver;
import com.gensdai.leliang.retrofitUtils.ModelFilteredFactory;
import com.gensdai.leliang.utils.PreferencesUtils;
import com.gensdai.leliang.view.NoScrollListView;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by zsxx on 2017/9/21.
 */

public class ActivityApplyResultInfo extends BaseActivity {

    public static final String EXTRA_ORDERID = "extraOrderId";
    @BindView(R.id.serviceTimeLable)
    TextView serviceTimeLable;
    @BindView(R.id.serviceTimename)
    TextView serviceTimename;
    @BindView(R.id.applytimeLayout)
    RelativeLayout applytimeLayout;
    @BindView(R.id.moneylable)
    TextView moneylable;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.amountMoney)
    TextView amountMoney;
    @BindView(R.id.paytypeLable)
    TextView paytypeLable;
    @BindView(R.id.payMoney)
    TextView payMoney;
    @BindView(R.id.payamountMoney)
    TextView payamountMoney;
    @BindView(R.id.paytypeLayout)
    RelativeLayout paytypeLayout;
    @BindView(R.id.groutbeiLayout)
    TextView groutbeiLayout;
    @BindView(R.id.groutbei)
    TextView groutbei;
    @BindView(R.id.productList)
    NoScrollListView productList;
    @BindView(R.id.postTimeLable)
    TextView postTimeLable;
    @BindView(R.id.postTime)
    TextView postTime;
    @BindView(R.id.applynoLable)
    TextView applynoLable;
    @BindView(R.id.applyno)
    TextView applyno;
    @BindView(R.id.applyNoLayout)
    RelativeLayout applyNoLayout;

    KProgressHUD hud;
    @BindView(R.id.applyimg)
    ImageView applyimg;
    private String orderid;

    @Override
    protected String getTitleStr() {
        return "退款详情";
    }

    @Override
    protected boolean isShowBackButton() {
        return true;
    }

    @Override
    protected boolean isCustomTitle() {
        return false;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_apply_result;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderid = getIntent().getStringExtra(EXTRA_ORDERID);
        ButterKnife.bind(this);

        hud = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在处理...")
                .setCancellable(true)
                .setAnimationSpeed(1)
                .setDimAmount(0.5f);

        if (null != orderid)
            getApplyResultInfo(orderid);
    }

    private void getApplyResultInfo(String oid) {
        String userid = PreferencesUtils.getStringPreference(this, OrderPreperActivity.prefreceName, OrderPreperActivity.prefrece_userid, ""); //获取用户id没有登陆就是没有

        Map<String, String> params = new HashMap<>();
        params.put("orderId", oid);
        params.put("userId", userid);
        ModelFilteredFactory.compose(RetrofitFactory.getInstance().API().APPLYDETAIL(params))
                .compose(this.<BaseParse<ApplyResultDetailBean>>bindUntilEvent(ActivityEvent.DESTROY)) //跟生命周期绑定一起
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }
                        if (hud != null && !hud.isShowing()) {
                            hud.show();
                        }
                        Log.d("accept", "accept");
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ApplyResultDetailBean>(this) {

                    @Override
                    public void onRequestEnd() {
                        super.onRequestEnd();
                        if (hud != null && hud.isShowing()) {
                            hud.dismiss();
                        }
                    }

                    @Override
                    public void onHandleSuccess(ApplyResultDetailBean info) {
                        //处理得到的数据
                        initView(info);
                    }

                    @Override
                    public void onHandleError(int code, String message) {

                    }
                });
    }

    private void initView(ApplyResultDetailBean info) {

        if (TextUtils.equals("1", info.getRefundStatus())){
            applyimg.setImageResource(R.mipmap.result_applying);
            applytimeLayout.setVisibility(View.GONE);
            paytypeLayout.setVisibility(View.GONE);
            applyNoLayout.setVisibility(View.GONE);


        }else if(TextUtils.equals("2", info.getRefundStatus())){
            applyimg.setImageResource(R.mipmap.result_applysuccess);
            applytimeLayout.setVisibility(View.VISIBLE);
            serviceTimename.setText(info.getFinishTime());

            paytypeLayout.setVisibility(View.VISIBLE);
            switch (info.getPayType()){
                case 1:
                    paytypeLable.setText("退回支付宝");
                    break;
                case 2:
                    paytypeLable.setText("退回成长金");
                    break;
                case 3:
                    paytypeLable.setText("退回到微信");
                    break;
            }

            payMoney.setText("￥" + info.getOtherAmount());
            payamountMoney.setText("(含邮费￥" + info.getTransferFee() + ")");

            applyNoLayout.setVisibility(View.VISIBLE);
            applyno.setText(info.getRefundNo());



        }

        postTime.setText(info.getCreateTime());
        money.setText("￥" + info.getAmount());
        amountMoney.setText("(含邮费￥" + info.getTransferFee() + ")");
        groutbei.setText("￥" + info.getGrouthBei());


        if (info.getOrderDetails() != null && info.getOrderDetails().size() > 0) {
            SimpleOrderInfoProductListAdapter adapter = new SimpleOrderInfoProductListAdapter(this);
            productList.setAdapter(adapter);
            adapter.initAdd(info.getOrderDetails());
        }

        //置顶
        applyimg.setFocusable(true);
        applyimg.setFocusableInTouchMode(true);
        applyimg.requestFocus();
    }


}
